import requests
import utils
import threading
import time

# Load Url_file
url_file = open("url_list.txt", 'r')
urls = url_file.readlines()


# ex: 'jeffyan2000/Adapted-game-snake'
def get_star(url, id):
    req = requests.get(url="https://gitee.com/" + url)
    found = req.text.find("stargazers")
    current = found + len("stargazers")
    res = -1

    if found >= 0:
        res = ""
        while req.text[current] != '>':
            if req.text[current] in "0123456789":
                res += req.text[current]
            current += 1
    result_list[id] = int(res)


if __name__ == '__main__':
    result_list = [-1 for _ in range(len(urls))]
    threads = []
    s = time.time()
    for i, url in enumerate(urls):
        while url.endswith('\n'):
            url = url[:-1]

        t = threading.Thread(target=get_star, args=(url, i))
        threads.append(t)
        t.start()

    for thread in threads:
        thread.join()

    print(result_list)
    print("took " + str(time.time() - s) + " seconds")
    utils.is_result_file_exist(urls)  # check file before adding new result
    utils.result_add_to_file(result_list)  # add new result and save file
